package com.zhentao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.model.article.dto.ArticleDto;
import com.zhentao.model.article.dto.ArticleHomeDto;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.article.pojo.ApArticleConfig;
import com.zhentao.model.article.pojo.ApArticleContent;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.enums.AppHttpCodeEnum;
import com.zhentao.service.ApArticleContentService;
import com.zhentao.service.ArticleService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 桃
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {


    //设置size常量 ，当页面传递过来的>50的时候，默认设置的最大数是50
    private final static short MAX_PAGE_SIZE = 50;
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Override
    public ResponseResult load(Integer loadType, ArticleHomeDto dto) {

        //校验参数 -size
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }


        //如果size>50 size==size 如果size>50 size=50
        size = Math.min(size, MAX_PAGE_SIZE);
        dto.setSize(size);


        //校验参数 频道
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag("__all__");
        }


        //校验参数  时间

        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }

        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        //查询数据
        List<Article> articles = articleMapper.loadArticleList(dto, loadType);

        return ResponseResult.okResult(articles);


    }


    @Override
    public ResponseResult saveArticle(ArticleDto dto) {

        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        //判断文章的新增还是修改
        if (dto.getId() == null) {
            //保存文章基本信息
            articleMapper.insert(apArticle);
            //保存内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);

            //保存配置
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            apArticleConfigMapper.insert(apArticleConfig);

        } else {
            //修改文章
            articleMapper.updateById(apArticle);

            //修改内容
            LambdaQueryWrapper<ApArticleContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApArticleContent::getArticleId, dto.getId());
            ApArticleContent content = apArticleContentMapper.selectOne(lambdaQueryWrapper);
            content.setContent(dto.getContent());
            apArticleContentMapper.updateById(content);


            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(new LambdaQueryWrapper<ApArticleConfig>()
                    .eq(ApArticleConfig::getArticleId, apArticle.getId()));
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfigMapper.updateById(apArticleConfig);
        }


        //生成url 修改数据库
        apArticleContentService.createStaticUrl(apArticle.getId());
        System.out.println("apArticle +++++++++++++++++++++++++++++++++= " + apArticle.getId());

        return ResponseResult.okResult(apArticle.getId());
    }


}
